package com.jiuxian.scheduling.listener.zk;

import com.jiuxian.scheduling.tasks.SchedulerApplication;
import com.jiuxian.scheduling.vo.SchedingVO;
import org.I0Itec.zkclient.IZkDataListener;

/**
 * Created by wangjingqing on 2017/11/8.
 * zk节点数据监听
 * @author wangjingqing
 */
public class ZkDataListener implements IZkDataListener {
    @Override
    public void handleDataChange(String dataPath, Object data) throws Exception {
        if(data instanceof SchedingVO){
            SchedingVO schedingVO = (SchedingVO)data;
            if(schedingVO.isChange()){
                if(schedingVO.getState() == 0){//运行
                    //判断是否包含，包含不执行
                    if(!SchedulerApplication.containJob(schedingVO.getName())){
                        SchedulerApplication.addJob(schedingVO);
                    }
                }else if(schedingVO.getState() == 1){//暂停
                    SchedulerApplication.removeJob(schedingVO.getName());
                }
            }
        }
    }
    @Override
    public void handleDataDeleted(String dataPath) throws Exception {
        System.out.println("删除节点："+dataPath);
    }
}
